package team.future.joke.service.impl;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.core.query.Update.update;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import team.future.joke.constant.StatusConstants;
import team.future.joke.constant.TypeConstants;
import team.future.joke.dao.BaseMongoDao;
import team.future.joke.entity.Joke;
import team.future.joke.entity.JokeLike;
import team.future.joke.entity.JokeStore;
import team.future.joke.param.JokeParam;
import team.future.joke.param.Page;
import team.future.joke.service.JokeLikeService;
import team.future.joke.service.JokeService;
import team.future.joke.service.JokeStoreService;

/**
 * Title: JokeServiceImpl.java<br>
 * Description: <br>
 * Copyright (c) 未来工作室版权所有 2015    <br>
 * Create DateTime: 2015年9月2日 上午9:25:49 <br>
 * @author futurezhu
*/
@Service("jokeService")
public class JokeServiceImpl implements JokeService {

	@Autowired
	private JokeStoreService jokeStoreService;
	@Autowired
	private JokeLikeService jokeLikeService;
	@Autowired
	private BaseMongoDao<Joke> jokeDao;
	public Joke getJokeByTitle(String title) {
		return jokeDao.getObject(query(where("title").is(title)));
	}

	@Override
	public void insertJoke(Joke joke) {
		jokeDao.insertObject(joke);
	}

	@Override
	public boolean contain(String title) {
		return jokeDao.countNum(query(where("title").is(title))) > 0;
	}

	@Override
	public List<Joke> getJokeListPage(JokeParam param) {
		Page page = new Page(param.getPageIndex(), param.getPageSize());
		Criteria criteria = Criteria.where("status").is(StatusConstants.STATUS_JOKE_NORMAL).and("nid").gt(0);
		List<JokeStore> stores = jokeStoreService.getJokeStores(param.getUid());
		List<JokeLike> likes = jokeLikeService.getJokeLikes(param.getUid());
		Set<String> likeJokeIds = new HashSet<String>();
		Set<String> storeJokeIds = new HashSet<String>();
		for (JokeLike l : likes) {
			likeJokeIds.add(l.getJokeId());
		}
		for (JokeStore s : stores) {
			storeJokeIds.add(s.getJokeId());
		}
		Sort sort = null;
		if (TypeConstants.PAGE_TYPE_HOT.equals(param.getPageType())) {
			//热门 按照赞，收藏，分享，nid倒序
			sort = new Sort(Direction.DESC, "likes", "store", "shareWeixinpy", "shareWeixinpyq", "shareSinaweibo", "nid");
		} else if (TypeConstants.PAGE_TYPE_NEW.equals(param.getPageType())) {
			sort = new Sort(Direction.DESC, "nid");
		} else if (TypeConstants.PAGE_TYPE_STORE.equals(param.getPageType())) {
			criteria.and("_id").in(storeJokeIds.toArray(new Object[0]));
			sort = new Sort(Direction.DESC, "nid");
		} else if(TypeConstants.PAGE_TYPE_LIKE.equals(param.getPageType())){
			criteria.and("_id").in(likeJokeIds.toArray(new Object[0]));
			sort = new Sort(Direction.DESC, "nid");
		}
		if (StringUtils.isNotBlank(param.getType())) {
			criteria.and("type").is(param.getType());
		} else if (StringUtils.isNotBlank(param.getTitle())) {
			//类似数据库like实现
			criteria.and("title").is("/.*" + param.getTitle() + ".*/");
		} else if (StringUtils.isNotBlank(param.getId())) {
			criteria.and("_id").is(param.getId());
		}
		List<Joke> jokes = jokeDao.getObjectList(query(criteria).with(page).with(sort));
		for(Joke j:jokes){
			if (likeJokeIds.contains(j.getId())) {
				j.setIsLiked("Y");
			}
			if (storeJokeIds.contains(j.getId())) {
				j.setIsStored("Y");
			}
		}
		return jokes;
	}
	
	@Override
	public List<Joke> getJokeListForDetail(JokeParam param) {
		Sort sort = new Sort(Direction.DESC, "time");
		Criteria criteria = Criteria.where("status").is(StatusConstants.STATUS_JOKE_NORMAL).and("nid").is(0);
		List<Joke> jokes = jokeDao.getObjectList(query(criteria).with(sort));
		return jokes;
	}


	@Override
	public int updateJokeLikesAdd(String id) {
		Joke joke = jokeDao.getObject(query(where("_id").is(id)));
		return jokeDao.updateObject(query(where("_id").is(id)),update("likes", joke.getLikes()+1));
	}

	@Override
	public int updateJokeLikesMius(String id) {
		Joke joke = jokeDao.getObject(query(where("_id").is(id)));
		return jokeDao.updateObject(query(where("_id").is(id)),update("likes", joke.getLikes()-1));
	}

	@Override
	public void updateJoke(Joke joke) {
		jokeDao.updateObject(query(where("_id").is(joke.getId())), update("nid",joke.getNid()));
	}

	@Override
	public int updateJokeCollectAdd(String id) {
		Joke joke = jokeDao.getObject(query(where("_id").is(id)));
		return jokeDao.updateObject(query(where("_id").is(id)), update("store", joke.getStore() + 1));
	}

	@Override
	public int updateJokeCollectMius(String id) {
		Joke joke = jokeDao.getObject(query(where("_id").is(id)));
		return jokeDao.updateObject(query(where("_id").is(id)), update("store", joke.getStore() - 1));
	}

	@Override
	public int updateJokeSinaWeiboAdd(String id) {
		Joke joke = jokeDao.getObject(query(where("_id").is(id)));
		return jokeDao.updateObject(query(where("_id").is(id)), update("shareSinaweibo", joke.getShareSinaweibo() + 1));
	}

	@Override
	public int updateJokeWeixinpyAdd(String id) {
		Joke joke = jokeDao.getObject(query(where("_id").is(id)));
		return jokeDao.updateObject(query(where("_id").is(id)), update("shareWeixinpy", joke.getShareWeixinpy() + 1));
	}

	@Override
	public int updateJokeWeixinpyqAdd(String id) {
		Joke joke = jokeDao.getObject(query(where("_id").is(id)));
		return jokeDao.updateObject(query(where("_id").is(id)), update("shareWeixinpyq", joke.getShareWeixinpyq() + 1));
	}

	@Override
	public Joke getJoke(String id, String uid) {
		if (StringUtils.isNotBlank(uid)) {
			List<JokeStore> stores = jokeStoreService.getJokeStores(uid);
			List<JokeLike> likes = jokeLikeService.getJokeLikes(uid);
			Set<String> likeJokeIds = new HashSet<String>();
			Set<String> storeJokeIds = new HashSet<String>();
			for (JokeLike l : likes) {
				likeJokeIds.add(l.getJokeId());
			}
			for (JokeStore s : stores) {
				storeJokeIds.add(s.getJokeId());
			}
			Joke joke = jokeDao.getObject(query(where("_id").is(id)));
			if (likeJokeIds.contains(joke.getId())) {
				joke.setIsLiked("Y");
			}
			if (storeJokeIds.contains(joke.getId())) {
				joke.setIsStored("Y");
			}
			return joke;
		} else {
			Joke joke = jokeDao.getObject(query(where("_id").is(id)));
			return joke;
		}
	}

}
